#encoding:gbk
__author__ = 'administrator'
import os

import sys
import MySQLdb
reload(sys)
sys.setdefaultencoding('gbk')

if __name__ == '__main__':
    conn = MySQLdb.connect(host = "192.168.2.172",port=3306 ,user ="mars",passwd = "mars123",db="", charset="utf8")
    cur = conn.cursor()
    conn2 = MySQLdb.connect(host = "192.168.2.172", port=3306,user ="mars",passwd = "mars123",db="",  charset="utf8")
    cur2 = conn2.cursor()
    cur3=conn2.cursor()
    conn.select_db('newdb')
    conn2.select_db('newdb')
    count6=0
    count7=0
    count8=0
    countflag=0
    count = cur.execute('select a.*,b.LINE_CODE,b.COMPANY_CODE,b.COMPANY_NAME,b.FILIALE_NAME,b.FILIALE_CODE from table_export0801b a,line_info b where a.linename=b.line_name and a.linetype = \'TY\'  ')
    #sql1="SELECT * from yy_line_daily_0408e where work_date=%s and line_name = %s and daily_type=1"
    #sql3="update  yy_line_daily set KILOMETER=%s,KILOMETER_EMPTY='0', KILOMETER_DIESEL=%s,KILOMETER_TROLLEYBUS=%s,KILOMETER_ELECTRIC=%s,ENERGY_CONSUMPTION_DIESEL=%s,ENERGY_CONSUMPTION_ELECTRIC=%s where work_date=%s and LINE_NAME=%s"
    sql4="insert into yy_line_daily (work_date,line_name,daily_type,KILOMETER,KILOMETER_EMPTY,KILOMETER_DIESEL,KILOMETER_TROLLEYBUS,KILOMETER_ELECTRIC,ENERGY_CONSUMPTION_DIESEL,ENERGY_CONSUMPTION_ELECTRIC,PERSON_TIME_TOTAL,PERSON_TIME_PREFERENTIAL,PERSON_TIME_FREE,INCOME_TOTAL,INCOME_PREFERENTIAL,INCOME_FREE,LINE_CODE,COMPANY_CODE,COMPANY_NAME,FILIALE_NAME,FILIALE_CODE,DATA_STATE) values (%s,%s,'2',%s,'0',%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,'1')"
    print count
    # TY 2
    # JT 3

    result = cur.fetchall()
    for r in result:

        testvar=r[11]+r[12]+r[34]
        testvar=round(float(r[11])+float(r[12])+float(r[34]),2)
        cur3.execute(sql4,(r[1],r[0],str(testvar),r[11],r[12],r[34],r[16],r[36],r[5],r[6],r[7],r[8],r[9],r[10],r[39],r[40],r[41],r[42],r[43]))
        print count6 ,'---'
        count6=count6+1
        conn2.commit()
        #print count6 , '+++'

    print count6
    conn.commit()
    cur.close()
    conn.close()
    conn2.commit()
    cur2.close()
    conn2.close()
    cur3.close()
